package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinyougou.dao.SpecificationDao;
import com.pinyougou.dao.SpecificationOptionDao;
import com.pinyougou.entity.PageResult;
import com.pinyougou.entity.SpecificationDTO;
import com.pinyougou.pojo.Specification;
import com.pinyougou.pojo.SpecificationOption;
import com.pinyougou.sellergoods.service.SpecificationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SpecificationServiceImpl implements SpecificationService {
    @Autowired
    private SpecificationDao specificationDao;
    @Autowired
    private SpecificationOptionDao specificationOptionDao;

    @Override
    public List<Specification> findAll() {
        List<Specification> specificationList = specificationDao.selectAll();
        return specificationList;
    }

    @Override
    public PageResult<Specification> findByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Specification> specificationList = specificationDao.selectAll();
        PageInfo<Specification> pageInfo = new PageInfo<>(specificationList);
        PageResult<Specification> pageResult = new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
        return pageResult;
    }

    @Override
    public void save(SpecificationDTO specificationDTO) {
        Specification specification = specificationDTO.getSpecification();
        //插入完成后自动返回规格表的主键自增id
        specificationDao.insert(specification);
        List<SpecificationOption> specificationOptionList = specificationDTO.getSpecificationOptionList();
        specificationOptionList.forEach(specificationOption -> {
            //给规格选项设置规格id
            specificationOption.setSpecId(specification.getId());
            specificationOptionDao.insert(specificationOption);
        });
    }

    @Override
    public SpecificationDTO findById(Integer id) {
        Specification specification = specificationDao.selectByPrimaryKey(id);
        //根据规格id查询规格选项列表
        Example example = new Example(SpecificationOption.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("specId", id);
        List<SpecificationOption> specificationOptionList = specificationOptionDao.selectByExample(example);
        //组装规格与规格选项列表
        SpecificationDTO specificationDTO = new SpecificationDTO();
        specificationDTO.setSpecification(specification);
        specificationDTO.setSpecificationOptionList(specificationOptionList);
        return specificationDTO;
    }

    @Override
    public void update(SpecificationDTO specificationDTO) {
        Specification specification = specificationDTO.getSpecification();
        specificationDao.updateByPrimaryKey(specification);
        //根据规格id删除原来的规格选项
        Example example = new Example(SpecificationOption.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("specId", specification.getId());
        specificationOptionDao.deleteByExample(example);
        //重新添加规格选项
        List<SpecificationOption> specificationOptionList = specificationDTO.getSpecificationOptionList();
        specificationOptionList.forEach(specificationOption -> {
            specificationOption.setSpecId(specification.getId());
            specificationOptionDao.insert(specificationOption);
        });
    }

    @Override
    public void delete(Integer[] ids) {
        //根据规格id删除规格选项
        Example optionExample = new Example(SpecificationOption.class);
        Example.Criteria optionExampleCriteria = optionExample.createCriteria();
        optionExampleCriteria.andIn("specId",Arrays.asList(ids));
        specificationOptionDao.deleteByExample(optionExample);
        //根据规格id删除规格
        Example example = new Example(Specification.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", Arrays.asList(ids));
        specificationDao.deleteByExample(example);
    }

    @Override
    public PageResult<Specification> findByPage(Specification specification, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Example example = null;
        if (specification != null) {
            example = new Example(Specification.class);
            Example.Criteria criteria = example.createCriteria();
            if (StringUtils.isNotBlank(specification.getSpecName())) {
                criteria.andLike("specName", "%" + specification.getSpecName() + "%");
            }
        }
        List<Specification> specificationList = specificationDao.selectByExample(example);
        PageInfo<Specification> pageInfo = new PageInfo<>(specificationList);
        PageResult<Specification> pageResult = new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
        return pageResult;
    }

    @Override
    public List<Map<Integer,String>> selectOptions() {
        List<Map<Integer,String>> options=specificationDao.selectOptions();
        return options;
    }
}
